//package com.ev;
///**
// * @author : CJH
// * @version 创建时间：2021年7月28日 下午2:00:04
// */
//
//import java.io.File;
//import java.io.Serializable;
//import java.io.StringWriter;
//import java.nio.charset.Charset;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//import org.geotools.data.FeatureWriter;
//import org.geotools.data.Transaction;
//import org.geotools.data.shapefile.ShapefileDataStore;
//import org.geotools.data.shapefile.ShapefileDataStoreFactory;
//import org.geotools.data.simple.SimpleFeatureIterator;
//import org.geotools.data.simple.SimpleFeatureSource;
//import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
//import org.geotools.geojson.feature.FeatureJSON;
//import org.opengis.feature.simple.SimpleFeature;
//import org.opengis.feature.simple.SimpleFeatureType;
//import org.opengis.referencing.crs.CoordinateReferenceSystem;
//
//import com.vividsolutions.jts.geom.Geometry;
//import com.vividsolutions.jts.geom.MultiLineString;
//
//import net.sf.json.JSONObject;
//
//public class ChangeShp1 {
//    public static void main(String[] args) {
//        read();
//    }
//
//    public static void read() {
//        try {
//
//            String shpPath = "C:\\Users\\55072\\Desktop\\新建文件夹\\Desktop\\LINE.shp";
//            String shpPath1 = "C:\\Users\\55072\\Desktop\\新建文件夹\\Desktop\\POINT.shp";
//            File shapeFile = new File(shpPath);
//            ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());
//            SimpleFeatureType schema = store.getSchema();
//            CoordinateReferenceSystem coordinateReferenceSystem = schema.getCoordinateReferenceSystem();
//            File file = new File("C:\\Users\\55072\\Desktop\\新建文件夹\\Export_OutputNew7.shp");
//            Map<String, Serializable> params = new HashMap<String, Serializable>();
//            params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
//
//            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(params);
//
//            // ds.setCharset(Charset.forName("UTF-8"));
//            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
//            tb.setCRS(coordinateReferenceSystem);
//            tb.setName("shapefile");
//            tb.add("OBJECTID", String.class);
//            tb.add("LineWt", Integer.class);
//            tb.add("Layer", String.class);
//            tb.add("高", Double.class);
//
//            tb.add("StartH", Double.class);
//            tb.add("EndH", Double.class);
//            tb.add("StartDepth", Double.class);
//            tb.add("EndDepth", Double.class);
//            tb.add("Calibre", Double.class);
//            tb.add("Type", String.class);
//            tb.add("ProName", String.class);
//            tb.add("Property", String.class);
//            tb.add("PressureL", String.class);
//            tb.add("StartPoint", String.class);
//            tb.add("EndPoint", String.class);
//            tb.add("the_geom", MultiLineString.class);
//            ds.createSchema(tb.buildFeatureType());
//            ds.setCharset(Charset.forName("GBK"));
//
//            FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = ds
//                    .getFeatureWriter(Transaction.AUTO_COMMIT);
//
//            // 设置编码
//            Charset charset = Charset.forName("GBK");
//            store.setCharset(charset);
//            SimpleFeatureSource sfSource = store.getFeatureSource();
//            SimpleFeatureIterator sfIter = sfSource.getFeatures().features();
//            File shapeFile1 = new File(shpPath1);
//            ShapefileDataStore store1 = new ShapefileDataStore(shapeFile1.toURI().toURL());
//            // 设置编码
//
//            store1.setCharset(charset);
//            SimpleFeatureSource sfSource1 = store1.getFeatureSource();
//            // SimpleFeatureIterator sfIter1 = sfSource1.getFeatures().features();
//            Object[] objects = sfSource1.getFeatures().toArray();
//            // 从ShapeFile文件中遍历每一个Feature，然后将Feature转为GeoJSON字符串
//            int cout = 0;
//            while (sfIter.hasNext()) {
//                SimpleFeature feature = (SimpleFeature) sfIter.next();
//                // Feature转GeoJSON
//                FeatureJSON fjson = new FeatureJSON();
//                StringWriter writer = new StringWriter();
//                fjson.writeFeature(feature, writer);
//                String sjson = writer.toString();
//                JSONObject object = JSONObject.fromObject(sjson);
//                JSONObject properties = object.getJSONObject("properties");
//                JSONObject geometry = object.getJSONObject("geometry");
//
//                MultiLineString geom = (MultiLineString) feature.getAttribute("the_geom");
//                String typeObject = geometry.getString("type").toUpperCase();
//                List<JSONObject> startGeometries = new ArrayList<JSONObject>();
//                List<JSONObject> endGeometries = new ArrayList<JSONObject>();
//
//                for (Object feature1 : objects) {
//
//                    SimpleFeature feature2 = (SimpleFeature) feature1;
//                    Geometry geom1 = (Geometry) feature2.getAttribute("the_geom");
////					if (geom.intersects(geom1))
//                    if (geom.getGeometryN(0).getCoordinates()[0].distance(geom1.getCoordinate()) <= 0.000001) {
//
//                        StringWriter writer2 = new StringWriter();
//                        FeatureJSON fjson1 = new FeatureJSON();
//                        fjson1.writeFeature(feature2, writer2);
//                        String sjson2 = writer2.toString();
//                        JSONObject object2 = JSONObject.fromObject(sjson2);
//                        JSONObject properties2 = object2.getJSONObject("properties");
//                        startGeometries.add(properties2);
//
//                    }
//                    if (geom.getGeometryN(0).getCoordinates()[1].distance(geom1.getCoordinate()) <= 0.000001) {
//
//                        StringWriter writer2 = new StringWriter();
//                        FeatureJSON fjson1 = new FeatureJSON();
//                        fjson1.writeFeature(feature2, writer2);
//                        String sjson2 = writer2.toString();
//                        JSONObject object2 = JSONObject.fromObject(sjson2);
//                        JSONObject properties2 = object2.getJSONObject("properties");
//                        endGeometries.add(properties2);
//
//                    }
//
//                }
//                JSONObject startFeature = new JSONObject();
//                JSONObject endFeature = new JSONObject();
//
//                for (int i = 0; i < startGeometries.size(); i++) {
//                    for (int j = 0; j < endGeometries.size(); j++) {
//                        Double startNum = Double.parseDouble(startGeometries.get(i).get("序号").toString());
//                        Double endNum = Double.parseDouble(endGeometries.get(j).get("序号").toString());
//
//                        if (Math.abs(startNum - endNum) <= 1) {
//                            startFeature = startGeometries.get(i);
//                            endFeature = endGeometries.get(j);
//                        }
//
//                    }
//
//                }
//                if (properties.getString("OBJECTID").equals("495")) {
//                    System.out.println();
//                }
//                if (startGeometries.size() == 0 && endGeometries.size() > 0) {
//                    startFeature = endGeometries.get(0);
//                    endFeature = endGeometries.get(0);
//                }
//                if (startGeometries.size() > 0 && endGeometries.size() == 0) {
//                    startFeature = startGeometries.get(0);
//                    endFeature = startGeometries.get(0);
//                }
//
////				Dist dist = new Dist();
////				dist.setCity(properties.getString("shi"));
////				dist.setCode(properties.getString("XZQDM"));
////				dist.setCountry(properties.getString("XZQMC"));
////				dist.setCounty(properties.getString("xian"));
////				dist.setCreatedate(new Date());
////				dist.setProvince("辽宁省");
////				dist.setShape(geoString);
////				dist.setTown(properties.getString("zhen1"));
//
//                // System.out.println(geoString);
////Geometry geom = (Geometry) feature.getAttribute("the_geom");//
//                // System.out.println(object);
//
//                tb.add("the_geom", MultiLineString.class);
//                SimpleFeature featureNew = featureWriter.next();
//                featureNew.setAttribute("OBJECTID", properties.getString("OBJECTID"));
//                featureNew.setAttribute("LineWt", properties.getString("LineWt"));
//                featureNew.setAttribute("Layer", properties.getString("Layer"));
//                featureNew.setAttribute("高", properties.getString("Elevation"));
//                if (startFeature.toString().equals("{}")) {
//                    System.out.println();
//                }
//                if (!startFeature.toString().equals("{}")) {
//                    featureNew.setAttribute("StartH", startFeature.getString("地面标高"));
//                    featureNew.setAttribute("EndH", endFeature.getString("地面标高"));
//                    featureNew.setAttribute("StartDepth", startFeature.getString("埋深（m）"));
//                    featureNew.setAttribute("EndDepth", endFeature.getString("埋深（m）"));
//                    Pattern pattern = Pattern.compile("[^0-9]");
//                    Matcher matcher = pattern.matcher(startFeature.getString("管径或管件"));
//                    String all = matcher.replaceAll("");
//                    featureNew.setAttribute("Calibre", all);
//                    featureNew.setAttribute("Type", "圆管");
//                    featureNew.setAttribute("ProName", startFeature.getString("项目名称"));
//                    featureNew.setAttribute("Property", startFeature.getString("管道属性"));
//                    featureNew.setAttribute("PressureL", startFeature.getString("压力级制"));
//                    featureNew.setAttribute("StartPoint", startFeature.getString("点编号"));
//                    featureNew.setAttribute("EndPoint", endFeature.getString("点编号"));
//                }
//                featureNew.setAttribute("the_geom", geom);
//
//
//            }
//            featureWriter.write();
//            featureWriter.close();
//            ds.dispose();
//            System.out.println(cout);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
